package com.licq.tv.videoplayer;

import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.player.Player;

import java.util.Optional;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class VideoView extends SurfaceProvider implements Player.IPlayerCallback {
    Player player;
    HiLogLabel hiLogLabel = new HiLogLabel(HiLog.DEBUG, 1, "licq");

    public VideoView(Context context, SurfaceOps.Callback callback) {
        super(context);
        player = new Player(getContext());
        player.setPlayerCallback(this);
        Optional surfaceHolderOptional = getSurfaceOps();
        SurfaceOps surfaceHolder = (SurfaceOps) surfaceHolderOptional.get();
        surfaceHolder.addCallback(callback);
    }

    public void playAssets(String fileName, boolean isLooping, SurfaceOps holder) {
        try {
            //播放本地视频：
            player.setSource(getContext().getResourceManager().getRawFileEntry(fileName).openRawFileDescriptor());
            //播放在线视频：
            //player.setSource(new Source("http://jzvd.nathen.cn/6ea7357bc3fa4658b29b7933ba575008/fbbba953374248eb913cb1408dc61d85-5287d2089db37e62345123a1be272f8b.mp4"));
            player.setVideoSurface(holder.getSurface());
            player.enableSingleLooping(isLooping);
            player.enableScreenOn(true);
            player.prepare();
            initPlayViewSize();
            player.play();
        } catch (Exception e) {
            e.printStackTrace();
            HiLog.debug(hiLogLabel, e.toString());
        }
    }

    private void initPlayViewSize() {
        int videoWidth = player.getVideoWidth();
        int videoHeight = player.getVideoHeight();
        setWidth(MATCH_PARENT);
        setHeight(MATCH_PARENT);
//        HiLog.debug(hiLogLabel,"videoWidth:" + videoWidth + ", videoHeight:" + videoHeight);
//        if (videoWidth < videoHeight) {
//            double scale = screenHeight * 1.f / videoHeight;
//            double currHeight = videoHeight * scale;
//            double currWidth = videoWidth * scale;
//            setHeight(((int) currHeight));
//            setWidth(((int) currWidth));
//        } else {
//            double scale = screenWidth * 1.f / videoWidth;
//            double currHeight = videoHeight * scale;
//            double currWidth = videoWidth * scale;
//            setHeight(((int) currHeight));
//            setWidth(((int) currWidth));
//        }
    }

    @Override
    public void onPrepared() {

    }

    @Override
    public void onMessage(int i, int i1) {

    }

    @Override
    public void onError(int i, int i1) {

    }

    @Override
    public void onResolutionChanged(int i, int i1) {

    }

    @Override
    public void onPlayBackComplete() {

    }

    @Override
    public void onRewindToComplete() {

    }

    @Override
    public void onBufferingChange(int i) {

    }

    @Override
    public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

    }

    @Override
    public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

    }
}
